package com.inibb.app.service.admin.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inibb.app.converter.Db2VOConverter;
import com.inibb.app.converter.GenericCodeConverter;
import com.inibb.app.dao.JpaGenericDao;
import com.inibb.app.dao.api.IDao;
import com.inibb.app.pagination.dto.PaginationCriteria;
import com.inibb.app.po.admin.GenericCode;
import com.inibb.app.service.admin.IGenericCodeService;
import com.inibb.app.vo.GenericCodeSC;
import com.inibb.app.vo.GenericCodeVO;

@Transactional
@Service
public class GenericCodeServiceImpl implements IGenericCodeService{

	private static final long serialVersionUID = 1L;
	
	private IDao<GenericCode, Integer> genericDao;
	
	@PersistenceContext(unitName = "entityManager")
	private EntityManager entityManager ;
	
	@Autowired
	public void setEntityManagerFactory() {
		genericDao = new JpaGenericDao<GenericCode, Integer>(entityManager,GenericCode.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GenericCodeVO> searchCode() {
		GenericCodeSC sc = new GenericCodeSC();
		sc.setStartRow(0);
		sc.setPageSize(Integer.MAX_VALUE);
		List<Object[]> list = (List<Object[]>) genericDao.queryNativeSqlForList("admin", "searchPageCodes", sc);
		List<GenericCodeVO> gcList = new ArrayList<GenericCodeVO>();
		for(Object[] obj:list){
			gcList.add(Db2VOConverter.toVO4GenericCode(obj));
		}
		return gcList;
		
//		return GenericCodeConverter.toVOs(genericDao.getAll());
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<GenericCodeVO> searchCodeByParentId(final Integer parentId) {
		Map<String, Object> criteria = new HashMap<String, Object>();
		criteria.put("parentId", parentId);
		List<Object[]> list = (List<Object[]>) genericDao.queryNativeSqlForList("admin", "selectCodeByParentId", criteria);
		List<GenericCodeVO> gcList = new ArrayList<GenericCodeVO>();
		for(Object[] obj:list){
			gcList.add(Db2VOConverter.toVO4GenericCode(obj));
		}
		return gcList;
		
		/*List<GenericCode> GoodsTypes = dao.getHibernateTemplate().executeFind(new HibernateCallback<List<GenericCode>>() {

			@Override
			public List<GenericCode> doInHibernate(Session session) throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(GenericCode.class);
				criteria.add(Restrictions.eq("parentId", parentId));
				criteria.add(Restrictions.eq("status", GenericCodeConstant.IND_YES));
				return criteria.list();
			}

		});
		return GenericCodeConverter.toVOs(GoodsTypes);*/
	}

	@Override
	public void saveOrUpdate(GenericCodeVO vo) {
		if(vo.getCodeId()==null)
			genericDao.persist(GenericCodeConverter.toPO(vo));
		else
			genericDao.merge(GenericCodeConverter.toPO(vo));
	}

	@Override
	public void delete(GenericCodeVO vo) {
		genericDao.delete(vo.getCodeId());
	}

	@Override
	public void delete(Integer id) {
		genericDao.delete(id);
	}
	public int countPageCodes(GenericCodeSC paginationCriteria) {
		BigInteger total = (BigInteger) genericDao.queryNativeSqlForObject("admin", "countPageCodes", paginationCriteria);
		
		return total.intValue();
	}

	public List<GenericCodeVO> searchPageCodes(GenericCodeSC paginationCriteria) {
		List<Object[]> list = (List<Object[]>) genericDao.queryNativeSqlForList("admin", "searchPageCodes", paginationCriteria);
		List<GenericCodeVO> gcList = new ArrayList<GenericCodeVO>();
		for(Object[] obj:list){
			gcList.add(Db2VOConverter.toVO4GenericCode(obj));
		}
		return gcList;
	}
	public int countPageCodes(PaginationCriteria paginationCriteria) {
		BigInteger total = (BigInteger) genericDao.queryNativeSqlForObject("admin", "countPageCodes", paginationCriteria);
		
		return total.intValue();
	}

	public List<GenericCodeVO> searchPageCodes(PaginationCriteria paginationCriteria) {
		List<Object[]> list = (List<Object[]>) genericDao.queryNativeSqlForList("admin", "searchPageCodes", paginationCriteria);
		List<GenericCodeVO> gcList = new ArrayList<GenericCodeVO>();
		for(Object[] obj:list){
			gcList.add(Db2VOConverter.toVO4GenericCode(obj));
		}
		return gcList;
	}

	@Override
	public GenericCodeVO getCode(Integer id) {
		
		return GenericCodeConverter.toVO(genericDao.get(id));
	}
	@SuppressWarnings("unchecked")
	public GenericCodeVO searchCodeTypeByCode(final String code) {
		Map<String, Object> criteria = new HashMap<String, Object>();
		criteria.put("code", code);
		
		Object[] obj = (Object[]) genericDao.queryNativeSqlForObject("admin", "findCodeTypeByCode", criteria);
		
		return Db2VOConverter.toVO4GenericCode(obj);
		
		/*List<GenericCode> shops = dao.getHibernateTemplate().executeFind(new HibernateCallback<List<GenericCode>>() {

			@Override
			public List<GenericCode> doInHibernate(Session session) throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(GenericCode.class);
				criteria.add(Restrictions.isNull("parentId"));
				criteria.add(Restrictions.eq("code", code));
				return criteria.list();
			}
		});
		if(CollectionUtils.isNotEmpty(shops)&&shops.size()>0)
			return GenericCodeConverter.toVO(shops.get(0));
		return null;*/
	}
	
	public GenericCodeVO searchCodeByTypeAndCode(final String type,final String code) {
		Map<String, Object> criteria = new HashMap<String, Object>();
		criteria.put("type", type);
		criteria.put("code", code);
		
		Object[] obj = (Object[]) genericDao.queryNativeSqlForObject("admin", "findCodeByTypeAndCode", criteria);
		
		return Db2VOConverter.toVO4GenericCode(obj);
	}
	@SuppressWarnings("unchecked")
	public List<GenericCodeVO> searchCodeType() {
		List<Object[]> list = (List<Object[]>) genericDao.queryNativeSqlForList("admin", "selectCodeType", null);
		List<GenericCodeVO> gcList = new ArrayList<GenericCodeVO>();
		for(Object[] obj:list){
			gcList.add(Db2VOConverter.toVO4GenericCode(obj));
		}
		
		/*List<GenericCode> shops = dao.getHibernateTemplate().executeFind(new HibernateCallback<List<GenericCode>>() {

			@Override
			public List<GenericCode> doInHibernate(Session session) throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(GenericCode.class);
				criteria.add(Restrictions.isNull("parentId"));
				return criteria.list();
			}

		});*/
		return gcList;
	}
}
